package com.jd.myaaaccount;

import java.util.ArrayList;
import java.util.List;

import com.jd.myaaaccount.model.Common;
import com.jd.myaaaccount.util.ErrorCode;
import com.jd.myaaaccount.util.OperateCode;

import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.content.Intent;
import android.graphics.Color;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class MainActivity extends BaseActivity {

	private ViewPager vpMain;
	private List<View> vList;

	private TextView tvAddRecord;
	private TextView tvScanRecord;
	private TextView tvSettleRecord;
	private TextView tvMe;
	private List<TextView> tvList;

	PagerAdapter pa;
	AddDayBookPage arp;
	DayBookPage mrp;
	SettlePage sp;
	MePage mp;

	Handler handler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		initControl();

		handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case OperateCode.CHECK_LOGIN:
					if (msg.arg1 == ErrorCode.SUCCESS) {
						vpMain.setAdapter(pa);
					} else if (msg.arg1 == ErrorCode.NOT_LOGIN) {
						Intent intent = new Intent();
						intent.setClass(MainActivity.this, LoginActivity.class);
						startActivity(intent);
						finish();
					} else {
						Common.showErrorInfo(MainActivity.this, msg.arg1, "", 0);

						// Intent intent = new Intent();
						// intent.setClass(MainActivity.this,
						// LoginActivity.class);
						// startActivity(intent);
						// finish();
					}
					break;
				default:
					break;
				}
			}
		};

		vpMain.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				setTabTextViewStyle(arg0);

				if (arg0 == 0 && arp == null) {
					arp = new AddDayBookPage();
					arp.iniPage(MainActivity.this);
				} else if (arg0 == 1 && mrp == null) {
					mrp = new DayBookPage();
					mrp.iniPage(MainActivity.this);
				} else if (arg0 == 2 && sp == null) {
					sp = new SettlePage();
					sp.iniPage(MainActivity.this);
				} else if (arg0 == 3 && mp == null) {
					mp = new MePage();
					mp.iniPage(MainActivity.this);
				}
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}
		});

		pa = new PagerAdapter() {

			@Override
			public boolean isViewFromObject(View arg0, Object arg1) {
				return arg0 == arg1;
			}

			@Override
			public int getCount() {
				return vList.size();
			}

			@Override
			public void destroyItem(ViewGroup container, int position,
					Object object) {
				container.removeView(vList.get(position));
			}

			@Override
			public Object instantiateItem(ViewGroup container, int position) {
				container.addView(vList.get(position));

				if (position == 0 && arp == null) {
					arp = new AddDayBookPage();
					arp.iniPage(MainActivity.this);
				}
				// else if (position == 1) {
				// MyRecordPage mrp = new MyRecordPage();
				// mrp.iniPage(MainActivity.this);
				// } else if (position == 2) {
				// SettlePage sp = new SettlePage();
				// sp.iniPage(MainActivity.this);
				// } else if (position == 3) {
				// MePage mp = new MePage();
				// mp.iniPage(MainActivity.this);
				// }
				return vList.get(position);
			}
		};

		// vpMain.setAdapter(pa);

		// 添加记录
		tvAddRecord.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				vpMain.setCurrentItem(0);
			}
		});
		// 账单
		tvScanRecord.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				vpMain.setCurrentItem(1);
			}
		});

		// 结账
		tvSettleRecord.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				vpMain.setCurrentItem(2);
			}
		});

		// 我
		tvMe.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				vpMain.setCurrentItem(3);
			}
		});

		// 检测是否登录
		new Thread(new Runnable() {

			@Override
			public void run() {
				Common.isLogin(MainActivity.this, handler);
			}
		}).start();

		// 开启消息接收服务
		Intent intent = new Intent("com.jd.myaaaccount.action.SUBSCRIBE");
		startService(intent);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		// getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	private void initControl() {
		LayoutInflater li = getLayoutInflater();
		vList = new ArrayList<View>();
		vList.add(li.inflate(R.layout.activity_add_daybook, null));
		vList.add(li.inflate(R.layout.activity_daybook, null));
		vList.add(li.inflate(R.layout.activity_settle, null));
		vList.add(li.inflate(R.layout.activity_me, null));
		vpMain = (ViewPager) findViewById(R.id.vpMain);

		tvList = new ArrayList<TextView>();
		tvAddRecord = (TextView) findViewById(R.id.tvAddRecord);
		tvList.add(tvAddRecord);
		tvScanRecord = (TextView) findViewById(R.id.tvScanRecord);
		tvList.add(tvScanRecord);
		tvSettleRecord = (TextView) findViewById(R.id.tvSettleRecord);
		tvList.add(tvSettleRecord);
		tvMe = (TextView) findViewById(R.id.tvMe);
		tvList.add(tvMe);
	}

	/**
	 * 设置 导航栏默认样式
	 */
	private void setTabTextViewStyle(int index) {

		for (TextView item : tvList) {
			item.setTextColor(Color.BLACK);
		}
		tvList.get(index).setTextColor(Color.argb(0xFF, 0x00, 0xEE, 0x00));
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == OperateCode.SELECT_PICTURE) {
			if (arp != null) {
				Uri uri = data.getData();
				arp.setPiture(uri);
			}
		} else if (requestCode == OperateCode.CAPTURE) {
			if (arp != null) {
				byte[] bmp = data.getByteArrayExtra("bmp");
				arp.setPiture(bmp);
			}
		}
	}
}
